package teleradiologia.repository;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.security.providers.encoding.Md5PasswordEncoder;

import teleradiologia.domain.Patient;
import teleradiologia.domain.Technician;
import teleradiologia.utils.HibernateUtil;

public class PatientDaoImpl implements PatientDao {
	private org.springframework.security.providers.encoding.BaseDigestPasswordEncoder encoder = new Md5PasswordEncoder();

	@Override
	public long create(Patient patient) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		patient.getPerson().setPassword(
				encoder.encodePassword(patient.getPerson().getPassword(),
						patient.getPerson().getUsername()));

		long generatedId = (Long)session.save(patient);

		tx.commit();
		session.close();

		return ++generatedId;

	}

	@Override
	public void delete(Patient patient) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		session.delete(patient);

		tx.commit();
		session.close();
	}

	@Override
	public Patient getById(long id) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		Patient patient = (Patient) session.get(Patient.class, new Long(id));

		tx.commit();
		session.close();

		return patient;
	}

	@Override
	public void update(Patient patient) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		if (patient.getPerson().getPassword().length() < 20) {
			patient.getPerson().setPassword(
					encoder.encodePassword(patient.getPerson().getPassword(),
							patient.getPerson().getUsername()));
		}

		session.update(patient);

		tx.commit();
		session.close();

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Patient> getAll() {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		List<Patient> patiens = session.createQuery("from Patient").list();

		tx.commit();
		session.close();

		return patiens;
	}

	@Override
	public Patient getByPesel(String pesel) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		Patient patient = (Patient) session.createQuery(
				"from Patient patient where patient.pesel = :pesel").setString(
				"pesel", pesel).uniqueResult();

		tx.commit();
		session.close();

		return patient;
	}

	@Override
	public Patient getByIdPerson(long id) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		Patient patient = (Patient) session
				.createQuery(
						"from Patient patient where patient.person.id = :id")
				.setLong("id", id).uniqueResult();

		tx.commit();
		session.close();

		return patient;
	}
}
